const Koa = require('koa');
const cors = require('koa-cors');
const bodyParser = require('koa-bodyparser');
const multer = require('koa-multer');//加载koa-multer模块
const Router = require('koa-router');
const static = require('koa-static');

const router = new Router();


// 预约管理
const userManagement = require('./router/userManagement/user');

const app = new Koa();

app.use(static(__dirname + '/public'));
app.use(bodyParser());
app.use(cors());
router.get('/', ctx => {
  ctx.body = 'koa server!'
});


//文件上传
//配置
var storage = multer.diskStorage({
  //文件保存路径
  destination: function (req, file, cb) {
    cb(null, 'public/uploads/')
  },
  //修改文件名称
  filename: function (req, file, cb) {
    var fileFormat = (file.originalname).split(".");
    cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
  }
})
//加载配置
var upload = multer({ storage: storage });
//路由
router.post('/upload', upload.single('file'), async (ctx, next) => {
  ctx.body = {
    filename: ctx.req.file.filename,//返回文件名
    fullPath: `http://192.168.0.102:7001/uploads/${ctx.req.file.filename}`
  }
})


router.post('/login', ctx => {
  ctx.body = {
    code: 200,
    data: {
      token: 'admin'
    }
  }
});
router.get('/user/info', ctx => {
  ctx.body = {
    code: 200,
    data: {
      roles: ['admin'],
      permission: ['admin'],
      token: 'admin',
      introduction: '我是超级管理员',
      avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
      name: 'Super Admin'
    }
  }
});

router.use(userManagement);
 


app.use(router.routes()).use(router.allowedMethods())
app.listen(7001);